<template>
  <!-- 面板 -->
  <div class="pad">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="目录名称" style="padding: 0 30px 0 10px">
          <el-input v-model="formInline.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.state" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm()">清除</el-button>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="success" icon="el-icon-edit" @click="onSubmit"
            >新增目录</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 数据显示条数 -->
      <el-alert title="数据一共有12条" type="info" :closable="false" show-icon>
      </el-alert>
      <!-- 表格显示 -->
      <el-table :data="list" border style="width: 100%">
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column label="创建日期">
          <template v-slot="state">
            {{ dateHandler(state.row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="面试题数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="state">
            {{ state.row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template v-slot="state">
            <el-button @click="ford(state.row.id)" type="text" size="small">{{
              boolearValue
            }}</el-button>
            <el-button
              type="text"
              size="small"
              :disabled="booValue"
              @click="changelist(state)"
              >修改</el-button
            >
            <el-button
              :disabled="booValue"
              type="text"
              size="small"
              @click="deleteHandler(state.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="float: right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="pagege.pagesize"
        background
        layout="prev, pager, next, sizes,jumper"
        :total="totals"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      :title="checkvalue ? '新增目录' : '修改目录'"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="adlist">
        <el-form-item label="所属学科" :label-width="formLabelWidth">
          <el-select placeholder="请选择" v-model="adlist.subjectID">
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.subjectName"
              :value="item.subjectID"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" :label-width="formLabelWidth">
          <el-input
            v-model="adlist.directoryName"
            style="width: 224px"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addList(adlist, checkvalue)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, update, remove } from '@/api/hmmm/directorys.js'
const moment = require('moment')

export default {
  data () {
    return {
      pagege: {
        page: 1,
        pagesize: 10
      },
      formInline: {
        subjectName: '',
        state: ''
      },
      checkvalue: false,
      boolearValue: '禁用',
      booValue: false,
      list: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      adlist: {
        subjectID: '',
        directoryName: ''
      },
      ID: 1,
      totals: 0
    }
  },

  created () {
    this.listHandler()
  },
  methods: {
    resetForm () {
      this.formInline = {
        subjectName: '',
        state: ''
      }
      this.listHandler()
    },
    onSubmit () {
      this.dialogFormVisible = true
      this.checkvalue = true
    },
    handleClick (row) {
      console.log(row)
    },
    // 按钮的禁用和启用
    ford (id) {
      if (this.boolearValue === '启用') {
        this.boolearValue = '禁用'
        this.booValue = false
        this.list.state = 0
      } else {
        this.boolearValue = '启用'
        this.booValue = true
        this.list.state = 1
      }
    },
    // 获取页面的数据表格
    async listHandler () {
      const res = await list(this.pagege)
      console.log(res, 8989898)
      if (res.status === 200) {
        this.list = res.data.items
        this.totals = res.data.counts
      }
    },
    // 转换日期格式
    dateHandler (date) {
      let createTime = date
      createTime = moment(createTime).format('YYYY-MM-DD HH:mm:ss')
      return createTime
    },
    // 分页控制
    handleSizeChange (val) {
      this.pagege.pagesize = val
      console.log(val, 8971893)
      this.listHandler()
    },
    handleCurrentChange (val) {
      this.pagege.page = val
      console.log(val, 776888)
      console.log(this.pagege, 1213456789)
      this.listHandler()
    },
    // 确认按钮
    async addList (adlist, checkvalue) {
      if (checkvalue === true) {
        const res = await add(adlist)
        // 判断返回值请求是否成功,成功则刷新页面,清空表单
        if (res.status === 200) {
          this.listHandler()
          this.adlist.subjectID = ''
          this.adlist.directoryName = ''
        }
      } else {
        const data = {
          id: this.ID,
          subjectID: this.adlist.subjectID,
          directoryName: this.adlist.directoryName
        }
        const res = update(data)
        if (res.status === 200) {
          this.listHandler()
          this.adlist.subjectID = ''
          this.adlist.directoryName = ''
        }
      }
      this.dialogFormVisible = false
    },
    // 修改页面
    changelist (state) {
      this.checkvalue = false
      this.dialogFormVisible = true
      console.log(state, 1234)
      this.adlist.directoryName = state.row.directoryName
      this.ID = state.row.id
    },
    async deleteHandler (id) {
      const res = await remove(id)
      if (res.states === 200) {
        this.listHandler()
      }
    }
  }
}
</script>

<style scoped lang="scss">
.pad {
  margin: 10px 10px;
}
</style>
